home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / c4 / pro11 / isready.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-05  |  4.4 KB  |  127 lines

  1. /* ====================================================================
  2.  *  ISREADY.C - program to enhance batch files by checking to see if the
  3.  *  requested drive letter is ready - usable - without attempting to read
  4.  *  the device.  This offers the advantage of being able to prompt the
  5.  *  user without having DOS "ABORT, RETRY, FAIL" prompt given instead.
  6.  *  A zero errorlevel is returned if the drive is ready.  Otherwise, a
  7.  *  non-zero code is returned.
  8.  *  =============================================================
  9.  *  Copyright (C) 1991 Bruce E. Högman.  All Rights Reserved.
  10.  *  This program is dedicated to the public domain by the author.
  11.  *  =============================================================
  12.  *  RETURN CODES (DOS ERRORLEVEL IN .BAT FILES)
  13.  *
  14.  *  The following values are "OR'd" into the return code:
  15.  *  0:   OK, is ready and readable.
  16.  *  4:   not ready
  17.  *  8:   removable device (may be reported with 0 or 4 codes)
  18.  *  16:  bad arguments (none or wrong length or wrong value)
  19.  *  Example:  8: is removable and ready.
  20.  *           12: is removable and not ready.
  21.  *            4: is not removable and not ready.
  22.  *           16: bad arguments.
  23.  * ====================================================================
  24.  */
  25. #include <stddef.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <conio.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31. #include <dos.h>
  32. #include <io.h>
  33. #include "getdpb.h"
  34. DPB_DATA far *getdpb( void );
  35. int getdrive ( void );          /* returns drive: 0=a,25=z */
  36. int seldisk ( char dskltr );    /* returns int: 0=OK, -1=error */
  37. int seldiskn( int  dsknbr );    /* returns int: 0=OK, -1=error */
  38. #define DEBUG 0
  39. #define NOT_READY 4
  40. #define BADARGS 16
  41. #define OK 0
  42. #define FALSE 0
  43. #define TRUE ~FALSE
  44. #define IGNORE 0
  45. #define RETRY  1
  46. #define ABORT  2
  47. static char msg[80];
  48. static int drive_ready=TRUE;
  49. static char drive_ltr;
  50. int movable (int drv);
  51. #pragma warn -par
  52. int far hndlherx( int errval, int ax, int bp, int si)
  53. { static char *err_msg[] = {
  54.   "write protect",  "unknown unit",  "drive not ready",   "unknown command",
  55.   "data error (CRC)",  "bad request",  "seek error",  "unknown media type",
  56.   "sector not found",  "printer out of paper",  "write fault",  "read fault",
  57.   "general failure",  "reserved message",  "reserved message",
  58.   "invalid disk change" };
  59.   unsigned di; int drive; int errorno;
  60.   di = _DI;  drive_ready = FALSE;
  61.   if (ax < 0)
  62.   { sprintf(msg,"Error: device error on drive %c:\n",drive_ltr);
  63.   }
  64.   else
  65.   { drive = ax & 0x00FF;
  66.     errorno = di & 0x00FF;
  67.     sprintf(msg,"Error:  %s on drive %c:\n",
  68.           err_msg[errorno], 'A' + drive);
  69.   }
  70.   hardresume(IGNORE);
  71.   return IGNORE;
  72. }
  73. int far hndlherx( int errval, int ax, int bp, int si);
  74. int main (int argc, char *argv[])
  75. { int savedrive, rcseldisk, verbose=0, rc=0; DPB_DATA *dptr; int rcmovable;
  76.   int drvn; char svdrv;
  77.   if (argc < 2)
  78.   { printf("Syntax:  isready drive_letter [/v] \n");
  79.     printf("/v: verbose: display messages \n");
  80.     rc = BADARGS; goto pgm_exit;
  81.   }
  82.   if (argc > 2)
  83.   { if (stricmp(argv[2],"/v") == 0) verbose=TRUE;}
  84.   savedrive = getdrive();                  /* Remember current CWD drive */
  85.   #if DEBUG
  86.   svdrv = (char) (savedrive + (int) 'A');
  87.   printf("Current drive was %c, %d\n",svdrv,savedrive);
  88.   #endif
  89.   rcseldisk = seldisk(argv[1][0]);         /* Select new current drive */
  90.   if (rcseldisk < 0)
  91.   { rc = BADARGS; goto pgm_exit;}
  92.   drive_ltr = toupper(argv[1][0]); drvn = drive_ltr - 'A' + 1;
  93.   harderr(hndlherx); /* Define interrupt 24 handler */
  94.   dptr = getdpb();                         /* Read drive parm block */
  95.   rcmovable = movable(drvn)==0? 8: 0;
  96.   if (drive_ready == FALSE)
  97.   { rc = NOT_READY;
  98.     if (verbose)
  99.     { if (rcmovable) {  printf("Removable ");} else { printf("Fixed ");}
  100.       printf("drive %c is not ready\n",drive_ltr); printf("%s",msg);
  101.     }
  102.   }
  103.   else
  104.   { if (dptr->DPBV == '\xff') /* invalid DPB */
  105.     { rc = NOT_READY;
  106.       if (verbose) { printf("invalid DPB found\n"); }
  107.     }
  108.     else
  109.     { if (dptr->DPBV == '\x00')
  110.       { if (verbose)
  111.         { printf("valid DPB found\n");
  112.           printf("DPB drive: %xc\n",dptr->drv);
  113.           printf("DPB media: %xc\n",dptr->mdb);
  114.         }
  115.       }
  116.       else
  117.       { if (verbose) { printf("unknown DPB found\n");}
  118.       }
  119.     }
  120.   }
  121. pgm_exit:
  122.   if (rc != BADARGS) { rc |= rcmovable; }
  123.   rcseldisk = seldiskn(savedrive);
  124.   return rc;
  125. }
  126.  
  127.